module("Vector");

function vectorAlmostEqual(actual, expected, delta) {
    almostEqual(Vector.squaredDistance(actual, expected), 0, delta);
}

test("Squared length", function() {
	var v = new Vector(3, 5, 7);
	almostEqual(v.squaredLength(), 83);
});

test("Squared distance", function() {
	var a = new Vector(3, 5, 7);
	var b = new Vector(2, 7, 11);
	almostEqual(Vector.squaredDistance(a, b), 21);
});

test("Rotate zero degrees around a simple axis", function() {
	var original = new Vector(3, 5, 0);
	var axis = new Vector(0, 0, 1);
	var rotated = original.rotate(axis, 0);
	vectorAlmostEqual(rotated, original);
});

test("Rotate 180 degrees around a simple axis", function() {
	var original = new Vector(3, 5, 0);
	var axis = new Vector(0, 0, 1);
	// pi radians == 180 degrees
	var rotated = original.rotate(axis, Math.PI);
	var expected = new Vector(-3, -5, 0);
	vectorAlmostEqual(rotated, expected);
});

test("Rotation matrix, zero degrees around z axis", function() {
	var axis = new Vector(0, 0, 1);
	var m = axis.rotationMatrix(0);
	matrixAlmostEqual(m, Matrix.identity());
});

test("Rotation matrix, 180 degrees around z axis", function() {
	var axis = new Vector(0, 0, 1);
	var m = axis.rotationMatrix(Math.PI); // pi radians == 180 degrees
	var expected = new Matrix(-1, 0, 0,
				  0, -1, 0,
				  0, 0, 1);
	matrixAlmostEqual(m, expected);
});

test("Rotation matrix, 30 degrees around x axis", function() {
	var axis = new Vector(1, 0, 0);
	var degrees = 30;
	var radians = Math.PI * degrees / 180;
	var m = axis.rotationMatrix(radians);
	var sqrt3over2 = Math.sqrt(3) / 2;
	var expected = new Matrix(1, 0, 0,
				  0, sqrt3over2, -0.5,
				  0, 0.5, sqrt3over2);
	matrixAlmostEqual(m, expected);
});

test("Rotation matrix, 74 degrees around an oblique axis", function() {
	var axis = new Vector(-1.0 / 3,
			      2.0 / 3,
			      2.0 / 3);
	var degrees = -74;
	var radians = Math.PI * degrees / 180;
	var m = axis.rotationMatrix(radians);
	var expected = new Matrix(0.36, 0.48, -0.8,
				  -0.8, 0.6, 0,
				  0.48, 0.64, 0.6);
	matrixAlmostEqual(m, expected, 0.1);
});

test("Cross product of two vectors", function() {
	var a = new Vector(3, 5, 7);
	var b = new Vector(11, 13, 17);
	var crossProduct = Vector.crossProduct(a, b);
	var expected = new Vector(-6, 26, -16);
	vectorAlmostEqual(crossProduct, expected);
});
